<template>
	<view>
		<uni-nav-bar dark title="比赛" fixed :statusBar="true" left-icon="left" @clickLeft="onLeft">
			<block slot="right">
				<view class="city">
					<view @click="onCreated">
						<uni-icons color="#fff" fontFamily="CustomFont" type="plusempty" :size="20"></uni-icons>
					</view>
				</view>
			</block>
		</uni-nav-bar>
		<view>
			<uni-segmented-control :current="current" :values="items" style-type="text" active-color="#E96E40"
				@clickItem="onClickItem" />
			<uni-search-bar clearButton="none" cancelButton="none" @confirm="search"></uni-search-bar>

		</view>
		<view>
			<view class="list" v-for="(item, index) in list">
				<view class="time text-grey1">{{item.tournamentBeginTime}}</view>
				<view class="box" @click="goSetting(item.id,item)" @longtap="longtap(item.id)">
					<view class="branch">
						<view class="branchtime">
							<!-- {{funTime(item.tournamentBeginTime,'{3}:{4}')}} -->
							<text class="text-grey1">{{type[item.type]}}</text>
						</view>
						<view></view>
					</view>
					<view class="center">
						<view class="left">
							<view class="item">
								<view class="line">
									<view>
										<image v-if="item.oneTeam.teamLogo" :src="imageUrl+item.oneTeam.teamLogo"
											mode=""></image>
										<image src="@/static/user.jpg" v-else mode=""></image>
										{{item.oneTeam.teamName}}
									</view>
									<text>{{item.teamOneScore}}</text>
								</view>
								<view class="line">
									<view>

										<image v-if="item.twoTeam.teamLogo" :src="imageUrl+item.twoTeam.teamLogo"
											mode=""></image>
										<image src="@/static/user.jpg" v-else mode=""></image>
										{{item.twoTeam.teamName}}
									</view>
									<text>{{item.teamTwoScore}}</text>
								</view>
							</view>
						</view>
						<view class="rigt">
							<view>文字直播</view>
							<view>{{items[current]}}第{{item.subsection}}节</view>
						</view>
					</view>
					<view class="address">
						<uni-icons type="location-filled" size="20"></uni-icons>
						{{item.address}}
					</view>
				</view>
				<view class="btns flex justify-between " :class="{cur: current == 2}">
					<view v-if="current == 2" class="flex align-center justify-center">数据修改</view>
					<template v-if="portData == 'ios'">
						<view class="flex align-center justify-center" @click="goSetting(item.id,item)">技术统计</view>
					</template>
					<template v-if="portData == 'android'">
						<view class="flex align-center justify-center" @click="goSetting(item.id,item)">技术统计</view>
						<view class="flex align-center justify-center" @click.stop="($event) => onLive($event, item)">
							视频直播</view>
						<!-- <view class="flex align-center justify-center" @click="watchLive(item)">直播+技术</view> -->
					</template>
				</view>
			</view>

		</view>
		<uni-load-more :status="loadingType"></uni-load-more>
	</view>
</template>

<script>
	import {
		getseasonlist,
	} from '@/api/season/indx.js'
	import {
		getTournament,
		deletetournament,
		getImgCanvas
	} from '@/api/tournament/index.js'
	import {
		imageUrl
	} from '@/config.js'
	import {
		getToken
	} from '@/utils/auth'
	export default {
		data() {
			return {
				items: ['未开始', '进行中', '已结束'],
				list: [],
				id: '',
				type: {
					1: '小组赛',
					2: '淘汰赛',
					3: '四强赛',
					4: '半决赛',
					5: '决赛',
					6: '季军赛',
					7: '排位赛',
					8: '常规赛',
					9: '季后赛'
				},
				current: 0,
				typeSelect: 0,
				seasonName: '',
				pageData: {
					pageSize: 10,
					pageNum: 1
				},
				total: 0,
				loadingType: 'more',
				portData: 'ios'
			}
		},
		methods: {
			formatDateTime(date) {
				function padZero(num) {
					return num < 10 ? '0' + num : num;
				}

				var year = date.getFullYear();
				var month = padZero(date.getMonth() + 1);
				var day = padZero(date.getDate());
				var hours = padZero(date.getHours());
				var minutes = padZero(date.getMinutes());
				var seconds = padZero(date.getSeconds());

				return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
			},
			onLive(e, item) {
				e.preventDefault();


				// uni.navigateTo({
				// 	url: '/pages/competition/liver?tournamentBeginTime='+item.tournamentBeginTime +'&id=' + item.id
				// })
				// const liver = uni.requireNativePlugin('live-release-open');
				console.log(item.id)
				// liver.startLiveActivity('1',getToken(),item.id, '2020-10-1' ) 
				// console.log({
				// 	title: '测试',
				// 	token: getToken(),
				// 	tournamentId: item.id,
				// 	dateTime: '2024-06-01 08:00:00'
				// })
				let currentDateTime = new Date(new Date().getTime() + 86400000);
				uni.navigateTo({
					url: `/pages/liver/liver?id=${item.id}&time=${this.formatDateTime(currentDateTime)}`
				})
				return;

				liver.startLive({
					title: ' ',
					token: getToken(),
					tournamentId: item.id,
					dateTime: this.formatDateTime(currentDateTime),
					screenOrientation: 0
				}, sucess => {
					console.log(sucess)
				})
				// var Intent = plus.android.importClass("android.content.Intent");
				// var main = plus .android.runtimeMainActivity();
				// console.log(Intent.ACTION_VIEW)
				// var intent =new Intent(Intent.ACTION_VIEW); // 设置要开启的Activitv包类路径

				// // intent.setClassName("com.yundongshugen.live.ui.LiveActivity")
				// intent.putExtra("path","com.yundongshugen.live.ui.LiveActivity");

				// var CODE_REQUEST= 0;

				// main.startActivityForResult(intent,CODE_REQUEST);


				// var Intent = plus.android.importclass("android.content.Intent");
				// var Activity = plus.android.importclass("android.app.Activity");
				// var main = plus.android.runtimeMainActivity();

				// var intent = new Intent();
				// intent,setclassName(main,"com.yundongshugen.live.ui.LiveActivity");
				// main.startActivity(intent);

			},
			watchLive(item) {
				// getImgCanvas({tournamentId: item.id})
			},
			longtap(id) {
				uni.showModal({
					content: '确定删除吗？',
					success: (res) => {
						if (res.confirm) {
							deletetournament(id).then(res => {
								this.list = []
								this.pageData = {
									pageSize: 10,
									pageNum: 1
								}
								this.getList();
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			onCreated() {
				// uni.navigateTo({
				// 	url: '/pages/record/record'
				// })
				// return
				uni.removeStorageSync('onebasketballTeam')
				uni.removeStorageSync('twobasketballTeam')
				uni.navigateTo({
					url: '/pages/competition/index?id=' + this.id
				})
			},
			onRight() {
				uni.navigateTo({
					url: '/pages/competition/switch'
				})
			},
			onLeft() {
				uni.navigateBack()
			},
			funTime(date, template = '{0}年{1}月{2}日 {3}时{4}分{5}秒') {
				// 假设日期时间格式为 "YYYY-MM-DD HH:mm"
				let arr = date.match(/\d+/g);
				return template.replace(/\{(\d+)\}/g, (_, n) => {
					let item = arr[n] || '0';
					item.length < 2 ? item = '0' + item : null;
					return item;
				});
			},
			goSetting(id, item) {
				console.log(id, "------")
				uni.setStorageSync('info', item)
				uni.setStorageSync('subsection', item.subsection)
				if (this.current == 2) {
					uni.navigateTo({
						url: '/pages/record/realtime?id=' + id
					})
					return;
				}
				uni.navigateTo({
					// url: '/pages/competition/competitionSetting?id=' + id
					url: "/pages/competition/comoetitionSetting?id=" + id
				})
				uni.setStorageSync('seasonId', this.id)
			},
			onClickItem(val) {
				console.log(val)
				let {
					currentIndex
				} = val
				this.current = currentIndex
				this.pageData = {
					pageNum: 1,
					pageSize: 10,
				}
				this.list = []
				this.typeSelect = currentIndex;
				this.getList()
			},
			getList() {
				let contestId = uni.getStorageSync('contestId');
				getTournament({
					seasonId: this.id,
					contestId,
					state: this.typeSelect,
					seasonName: this.seasonName,
					...this.pageData
				}).then(res => {
					this.total = res.total
					this.list.push(...res.rows)
					if (this.list.length == this.total) {
						this.loadingType = 'no-more'
						return;
					}
				})
			},
			search(val) {
				console.log(val)
				this.seasonName = val.value;

				this.pageData = {
					pageNum: 1,
					pageSize: 10
				}
				this.list = []
				this.getList();
			},
		},
		onLoad({
			id
		}) {
			this.id = id;

		},
		async onReachBottom() {
			if (this.list.length == this.total) {
				this.loadingType = 'no-more'
				return;
			}
			this.loadingType = 'loading'
			this.pageData.pageNum += 1
			this.getList()
		},
		onShow() {
			this.pageData = {
				pageNum: 1,
				pageSize: 10
			}
			this.list = []
			this.getList()
			let portData = uni.getSystemInfoSync().platform;
			this.portData = portData;
		}
	}
</script>

<style lang="scss" scoped>
	.header-left {
		width: 20px;
		height: 20px;
	}

	.list {
		padding: 0rpx;

		.time {
			background-color: #F7FBFC;
			padding: 30rpx 20rpx;
			font-size: 24rpx;
		}

		.box {
			.branch {
				display: flex;
				justify-content: space-between;
				padding: 30rpx;

				.branchtime {
					font-size: 38rpx;
					font-weight: 600;

					text {
						font-weight: 400;
						font-size: 28rpx;
						margin-left: 10rpx;
					}
				}
			}

			.address {
				padding: 10rpx 30rpx;
				font-size: 24rpx;
				color: #919292;
			}

			.center {
				display: flex;
				padding: 0rpx 30rpx;

				.left {
					width: 70%;
					border-right: 1px solid #F7F7F7;
					padding-right: 30rpx;

					image {
						width: 60rpx;
						height: 60rpx;
						margin-right: 20rpx;
					}

					.item {
						display: flex;
						flex-direction: column;

						.line {
							display: flex;
							justify-content: space-between;
							align-items: center;
							margin-bottom: 20rpx;
							font-size: 26rpx;

							view {
								display: flex;
								align-items: center;

								image {
									border-radius: 50%;
								}
							}

							text {
								font-weight: 600;
								font-size: 34rpx;
							}
						}
					}
				}

				.rigt {
					display: flex;
					width: 30%;
					flex-direction: column;
					justify-content: center;
					align-items: center;
					font-size: 26rpx;
					color: $font-color;
				}
			}
		}

		.btns {
			padding: 20rpx 30rpx;

			view {
				width: calc((100% / 3) - 40rpx);
				height: 60rpx;
				border-radius: 30rpx;
				background-color: #F7F7F7;
				font-size: 24rpx;
				font-weight: 600;
			}

			&.cur {
				view {
					width: calc((100% / 4) - 40rpx);
				}

			}
		}
	}
</style>